package com.zq.controller;

import com.zq.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 简单的jdbc操作数据库
 */
@Controller
public class JdbcTemplateTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:bean.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
       /* List<Student> students = jdbcTemplate.query("select * from student3", new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setAddress(resultSet.getString("address"));
                student.setAge(resultSet.getInt("age"));
                student.setEnglish(resultSet.getInt("english"));
                student.setMath(resultSet.getInt("math"));
                return student;
            }
        });*/
        List<Student> students = jdbcTemplate.query("select * from student3", new BeanPropertyRowMapper<>(Student.class));

        students.forEach(System.out::println);
    }
}
